package com.yyy.gui.listener;

import com.yyy.entity.Category;
import com.yyy.gui.pannel.CategoryPanel;
import com.yyy.service.CategoryService;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author：yyy
 * @Package：com.yyy.gui.listener
 * @Project：HuTuBiill
 * @name：CategoryListener
 * @Date：2024/11/12 21:59
 * @Filename：CategoryListener
 */
public class CategoryListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        CategoryPanel p=CategoryPanel.instance;
        JButton b=(JButton) e.getSource();
        if(b==p.bAdd){
            String name=JOptionPane.showInputDialog(null);
            if(name.length()==0){
                JOptionPane.showMessageDialog(p,"分类名称不能为空");
                return;
            }
            new CategoryService().add(name);
        }

        if(b==p.bEdit){
            Category c=p.getSelectedCategory();
            int id=c.id;
            String name=JOptionPane.showInputDialog("修改分类的名称",c.name);
            if(name.length()==0){
                JOptionPane.showMessageDialog(p,"分类名称不能为空");
                return;
            }
            new CategoryService().update(id,name);

        }

        if(b==p.bDelete){
            Category c=p.getSelectedCategory();
            int id=c.id;
            if(c.recordNumber!=0){
                JOptionPane.showMessageDialog(p,"本分类下有消费记录存在，不能删除");
                return;
            }
            if(JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(p,"确认要删除吗？"))
                return;
            new CategoryService().delete(id);

        }


    }
}
